/****************************************************************
 * <strong>MFJsonlView</strong>
 *
 * <p>
 * MFJsonlView
 * </p>
 *
 * @author AndyZhang 2013
 * @version 0.1
 * ToDo:2013-3-3
 ***************************************************************/
package com.thinkwide.data.mvc.view;

import com.thinkwide.data.mvc.config.MFMVCReflect;
import com.thinkwide.data.mvc.core.IMFView;
import org.json.JSONArray;
import org.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

public class MFJsonView implements IMFView {
    private String url;
    private Object obj;
    private Map map;
    private List<?> list;

    public MFJsonView(String text) {
        this.url = text;
    }

    public MFJsonView(Object obj) {
        this.obj = obj;
    }

    public MFJsonView(Map map) {
        this.map = map;
    }

    public MFJsonView(List<?> list) {
        this.list = list;
    }

    public String getContentType() {

        return null;
    }

    public void render(Map<String, Object> map, HttpServletRequest request,
                       HttpServletResponse response) throws Throwable {

        if (obj != null) {
            JSONObject jsObj = MFMVCReflect.getEntityToJson(obj);
            response.getWriter().println(jsObj.toString());
        } else if (list != null) {
            JSONArray array = MFMVCReflect.getEntityListToJson(list);
            response.getWriter().println(array.toString());
        } else if (this.map != null) {
            JSONObject jsObj = MFMVCReflect.getMapToJson(this.map);
            response.getWriter().println(jsObj.toString());
        } else
            response.getWriter().println(url);
    }

}



